home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-28 | 4.4 KB | 209 lines | [TEXT/MPS ] |
- /*
- File: CreateObject.cp
-
- Copyright: © 1991-1994 by Apple Computer, Inc.
- All rights reserved.
-
- Part of the AOCE Sample SMSAM Package. Consult the license
- which came with this software for your specific legal rights.
-
- */
-
-
-
- #ifndef __CREATEOBJECT__
- #include "CreateObject.h"
- #endif
-
- #ifndef __STDIO__
- #include "StdIO.h"
- #endif
-
- #ifndef __STRING__
- #include "String.h"
- #endif
-
- #ifndef __MEMORY__
- #include "Memory.h"
- #endif
-
- #ifndef __RESOURCES__
- #include "Resources.h"
- #endif
-
- #ifndef __DEBUGASSERT__
- #include "DebugAssert.h"
- #endif
-
- #ifndef __DEBUGGINGGEAR__
- #include "DebuggingGear.h"
- #endif
-
- #pragma segment CreateObject
-
- /***********************************|****************************************/
-
- const OSType TDefaultConstructorIterator::kCodeResourceType = 'CODE';
- const OSType TDefaultConstructorIterator::kLookupResourceType = 'FcAd';
- const short TDefaultConstructorIterator::kLookupResourceID = 128;
-
- /***********************************|****************************************/
-
- TDefaultConstructorIterator::TDefaultConstructorIterator ():
- fResource ( nil ),
- fState ( 0 ),
- fMaxName ( nil ),
- fCurrentName ( nil ),
- fID ( -1 ),
- fOffset ( 0 )
- {
- SetupLookupResource ( GetLookupResource () );
- }
-
- /***********************************|****************************************/
-
- TDefaultConstructorIterator::TDefaultConstructorIterator ( const Handle data ):
- fResource ( nil ),
- fState ( 0 ),
- fMaxName ( nil ),
- fCurrentName ( nil ),
- fID ( -1 ),
- fOffset ( 0 )
- {
- SetupLookupResource ( (Handle) data );
- }
-
- /***********************************|****************************************/
-
- TDefaultConstructorIterator::~TDefaultConstructorIterator ()
- {
- if ( fResource )
- ::HSetState ( fResource, fState );
- }
-
- /***********************************|****************************************/
-
- void
- TDefaultConstructorIterator::SetupLookupResource ( Handle data )
- {
- if ( data )
- {
- fResource = data;
- fState = ::HGetState ( fResource );
- ::HLock ( fResource );
- fCurrentName = *fResource;
- fMaxName = fCurrentName + ::GetHandleSize ( fResource );
- fID = -1;
- fOffset = 0;
- }
- #if debug
- else
- {
- chris << "\n### The ‘FcAd’ could not be found in your application\n";
- chris << "### Please run the MakeFuncAddrRes tool on the app linkmap,\n";
- chris << "### then Rez the output file into the app after linking.\n\n";
- }
- #endif
- }
-
- /***********************************|****************************************/
-
- const char*
- TDefaultConstructorIterator::FirstConstructor ()
- {
- fCurrentName = *fResource;
- return NextConstructor ();
- }
-
- /***********************************|****************************************/
-
- const char*
- TDefaultConstructorIterator::NextConstructor ()
- {
- if ( fCurrentName < fMaxName )
- {
- char* name = fCurrentName;
-
- while ( *fCurrentName++ ) ;
-
- fID = *( (short*) fCurrentName );
- fCurrentName += sizeof ( fID );
- fOffset = *(unsigned long*) fCurrentName;
- fCurrentName += sizeof ( fOffset );
-
- return name;
- }
- else
- {
- return nil;
- }
- }
-
- /***********************************|****************************************/
-
- pascal void LoadSegment ( short ) = 0xA9F0;
-
- const Constructor
- TDefaultConstructorIterator::GetConstructorAddress () const
- {
- if ( fID > 0 )
- {
- // LoadSegment ( fID );
- Handle code = ::Get1Resource ( kCodeResourceType, fID );
-
- if ( code )
- {
- ::HLock ( code );
- return (Constructor) ( *code + fOffset );
- }
- else
- return nil;
- }
- else
- return nil;
- }
-
- /***********************************|****************************************/
-
- Handle
- TDefaultConstructorIterator::GetLookupResource ()
- {
- // we probably should make sure the current resource file
- // is the application resource file
-
- return ::Get1Resource ( kLookupResourceType, kLookupResourceID );
- }
-
- /***********************************|****************************************/
-
- static void*
- GetConstructorAddress ( const char* className )
- {
- TDefaultConstructorIterator iterator;
-
- for ( const char* currentName = iterator.FirstConstructor ();
- currentName != nil;
- currentName = iterator.NextConstructor () )
- {
- if ( ::strcmp ( currentName, className ) == 0 )
- return iterator.GetConstructorAddress ();
- }
-
- return nil;
- }
-
- /***********************************|****************************************/
-
- void*
- CreateObject ( const char* className )
- {
- Constructor constructor = (Constructor) ::GetConstructorAddress ( className );
-
- if ( constructor )
- return constructor ( nil );
- else
- return nil;
- }
-
- /***********************************|****************************************/
-